﻿using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Interface;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Domain.Impl
{
    public class PregnancyRepository:IPregnancyRepository
    {
        private readonly AgricultureDbContext _context;
        public PregnancyRepository(AgricultureDbContext context)
        {
            _context = context;
        }
        /// <summary>
        /// 妊检列表
        /// </summary>
        /// <returns></returns>
        public async Task<IQueryable<Sheeppregnancy>> GetSheeppregnancy()
        {
              return  _context.Sheeppregnancies.Where(c=>c.IsDel==false);
        }
        /// <summary>
        /// 添加种母妊检
        /// </summary>
        /// <param name="sheeppregnancy"></param>
        /// <returns></returns>
        public async Task<int> AddPregnancy(List<Sheeppregnancy> sheeppregnancy)
        {
            _context.Sheeppregnancies.AddRange(sheeppregnancy);
            return await _context.SaveChangesAsync();
        }
        /// <summary>
        /// 种母妊检删除
        /// </summary>
        /// <param name="pregnancyId"></param>
        /// <returns></returns>
        public async Task<int> DelePregnancy(string? eartag)
        {
            var entity = _context.Sheeppregnancies.FirstOrDefault(c => c.EarTag == eartag);
            if (entity != null)
            {
                entity.IsDel = true;
                entity.UpdateName = "赵思彤";
                entity.UpdateTime = DateTime.Now;
            }
            _context.Sheeppregnancies.Update(entity);
            var breed = _context.Breedinginfos.FirstOrDefault(c=>c.EarTag == eartag);
            if(breed != null)
            {
                breed.Isdel = false;
            }
            _context.Breedinginfos.Update(breed);
            //修改档案
            var sheepfile = _context.Sheepfilemodels.FirstOrDefault(c => c.EarTag == eartag);
            //根据档案耳号 获取分娩数据
            var pregnancies = _context.Sheeppregnancies.FirstOrDefault(s => s.EarTag == sheepfile.EarTag);
            if (sheepfile != null)
            {
                sheepfile.BuildingId = pregnancies.CurrentBuilding;
                sheepfile.ColumnId = pregnancies.CurrentField;
                sheepfile.ReproductionState = 1;
                sheepfile.IsPregnancy = 0;
            }
            _context.Sheepfilemodels.Update(sheepfile);
            return await _context.SaveChangesAsync();
        }
        /// <summary>
        /// 种母妊检修改批量
        /// </summary>
        /// <param name="sheeppregnancy"></param>
        /// <returns></returns>
        public async Task<int> UpdSheeppregnancys(List<Sheeppregnancy> sheeppregnancy)
        {
            _context.Sheeppregnancies.UpdateRange(sheeppregnancy);
            return await _context.SaveChangesAsync();
        }
        /// <summary>
        /// 种母妊检修改
        /// </summary>
        /// <param name="sheeppregnancy"></param>
        /// <returns></returns>
        public async Task<int> UpdSheeppregnancy(Sheeppregnancy sheeppregnancy)
        {
            _context.Sheeppregnancies.Update(sheeppregnancy);
            return await _context.SaveChangesAsync();
        }
    }
}
